#!/bin/bash

File_Status='./Step_Status.dat'

#line=$(head -n 1 $File_Status)
#Flag=${line:0:1}
#echo $line 
#echo $Flag

#exit
rm $File_Status

tot_time=1       # 1 hour  MAX run time 
let tot_time=$tot_time*60*60  # 1 hour  # update per 1 sec 

./run_step_1.sh &
count_time=0
Flag_Step=1
while [ $count_time -lt $tot_time ]
do
    #echo "$count_time"
    let count_time+=1
    sleep 1s
    if [ -f $File_Status ];then
        line=$(head -n 1 $File_Status)
	Flag=${line:0:1}
        if [[  $Flag -eq "1"  &&  Flag_Step -ne 2  ]];then
            echo "Finished Step I & start Step II"
            let Flag_Step=2
            ./run_step_2.sh &
        fi
        if [[  $Flag -eq "2"  &&  Flag_Step -ne 3  ]];then
            echo "Finished Step II & start Step III"
            let Flag_Step=3
            ./run_step_3.sh &
        fi
        if [[  $Flag -eq "3" ]];then
            break
	fi
    fi
done

echo " --- Finished ALL --- "
exit
